home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: in1.uu.net!allegra!alice!ark
- From: ark@research.att.com (Andrew Koenig)
- Subject: Re: SORTing problem c++
- Message-ID: <DLv3Bp.AHo@research.att.com>
- Organization: AT&T Research, Murray Hill NJ
- References: <310B0DD0.34F1@pi.net>
- Date: Sat, 27 Jan 1996 22:47:48 GMT
-
- In article <310B0DD0.34F1@pi.net> heggie <heggie@pi.net> writes:
-
- > what can I DO???
-
- > #include <stdio.h>
- > #include <stdlib.h>
- > #include <string.h>
-
- > int sort_function(char **a, char **b)
- > {
- > return( strcmp(*a, *b));
- > }
-
- > int main(int argc, char **argv)
- > {
- > int
- > x;
- >
- > qsort(argv, argc, sizeof(char*), sort_function);
- >
- > for (x = 0; x < argc; x++)
- > printf("%s\n", argv[x]);
- > return 0;
- > }
-
- This is not a valid program either in C or C++.
- However, the typical C++ compiler will diagnose
- the error and the typical C compiler will not.
-
- The problem is that the function passed to qsort
- is required to have parameters of type const void*
- and you gave it parameters of type char**.
-
- So you need to rewrite the function this way (for example):
-
- int sort_function(const void* a, const void* b)
- {
- const char** aa = (const char**) a;
- const char** bb = (const char**) b;
- return strcmp(*aa, *bb);
- }
-
- That should do it.
- --
- --Andrew Koenig
- ark@research.att.com
-